var express = require('express');
var router = express.Router();
let multiparty = require('multiparty')
var { sketchModel } = require("../model/model/niux")

router.post("/upload", (req, res) => {
  //创建一个表单对象
  let form = new multiparty.Form()
  //设计图片上传路径
  form.uploadDir = 'upload'
  //解析form对象
  form.parse(req, (err, file, data) => {
    if (err) {
      console.log(err);
    } else {
      let imgSrc = data.file[0].path
      sketchModel.create({ imgSrc: imgSrc })
        .then(() => {
          res.send({
            code: 200,
            msg: "图片上传成功",
            path: data.file[0].path
          });
        })
    }
  })
})

router.get("/getImg", async (req, res) => {
  let imgList = await sketchModel.find()
  let img = imgList[imgList.length - 1]
  console.log(img);
  
  res.send({
    code: 200,
    data: img
  })
})

module.exports = router;
